package com.yt.website.action;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yt.core.dao.PagingBean;
import com.yt.core.dao.filter.DynamicSQLFilterImpl;
import com.yt.core.dao.filter.QueryFilter;
import com.yt.order.api.interfaces.InstitutionService;
import com.yt.order.api.model.Institution;
import com.yt.website.util.PinyinUtil;
import com.yt.website.vo.OInstitutionVo;
import com.yt.website.action.base.BaseAction;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/institution")
public class InstitutionAction extends BaseAction {
	@Autowired
	private InstitutionService oInstitutionService;
	private static final List<String> provinceList = Lists.newArrayList("全部","北京","天津","河北","山西","内蒙古","辽宁","吉林","黑龙江","上海","江苏","浙江","安徽","福建","江西","山东","河南","湖北","湖南","广东","广西","海南","重庆","四川","贵州","云南","西藏","陕西","甘肃","青海","宁夏","新疆","台湾","澳门","香港");
	@RequestMapping("/index")
	public ModelAndView index(
			@RequestParam(value = "keyword", required = false) String keyword,
			@RequestParam(value = "province", required = false) String province,
			@RequestParam(value = "pageNo", required = false) Integer pageNo) {
		ModelAndView mav = new ModelAndView("institution/index");
		try {
			//查询一级医院
			QueryFilter filter = new DynamicSQLFilterImpl();
			filter.addFilter("Q_parentId_L_EQ", "0");
			if (StringUtils.isNotEmpty(province) && !"全部".equals(province)) {
				filter.addFilter("Q_province_S_EQ", province.trim());
				mav.addObject("province", province);
			}else{
				mav.addObject("province","全部");
			}
			filter.addFilter("Q_toWebsite_N_EQ", "1");
			if (StringUtils.isNotEmpty(keyword)) {
				filter.addFilter("Q_pinyin_S_LK", PinyinUtil.getPinyinJianPinToUpperCase(keyword.trim()));
				mav.addObject("keyword", keyword.trim());
			}
			filter.setSortField("province,city", false);
			pageNo = pageNo == null ? 1 : pageNo;
			int pageSize=12;
			filter.setPagingBean(new PagingBean((pageNo - 1) * pageSize, pageSize));
			List<Institution> institutionList = oInstitutionService.find(filter);

			List<OInstitutionVo> voList = Lists.newArrayList();
			for (Institution institution : institutionList) {
				OInstitutionVo vo = new OInstitutionVo();
				BeanUtils.copyProperties(institution, vo);
				if (institution.getImages() != null && !"".equals(institution.getImages())) {
					List<String> imageList = Splitter.on(";").omitEmptyStrings().splitToList(institution.getImages());
					if (imageList != null && imageList.size() > 0) {
						vo.setImages(imageList.get(0));
					}
				}
				voList.add(vo);
			}
			int total = oInstitutionService.count(filter);
			//区域
			mav.addObject("institutionList", voList);
			mav.addObject("provinceList",provinceList);
			mav.addObject("pageNo",pageNo);
			mav.addObject("total", total);
			mav.addObject("pageSize", pageSize);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}

	/**
	 * 详情页面
	 * 有分院的进入 分院列表，如果没有分院则进入详情页面
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping("/detail")
	public ModelAndView detail(@RequestParam(value = "id") Long id) {
		ModelAndView mav = null;
		try {
			//查询分院信息
			QueryFilter filter = new DynamicSQLFilterImpl();
			filter.addFilter("Q_parentId_L_EQ", id + "");
			filter.addFilter("Q_toWebsite_N_EQ", "1");
			filter.setSortField("province,city", false);
			filter.setPagingBean(null);
			//查询分院
			List<Institution> branchList = oInstitutionService.find(filter);
			Map<String, List<Institution>> branchMap = Maps.newLinkedHashMap();
			for (Institution branch : branchList) {
				List<Institution> tempList = branchMap.get(branch.getProvince());
				if (tempList == null) {
					tempList = Lists.newArrayList();
					tempList.add(branch);
					branchMap.put(branch.getProvince(), tempList);
				} else {
					tempList.add(branch);
					branchMap.put(branch.getProvince(), tempList);
				}
			}
			if (branchList != null && branchList.size() > 0) {
				mav = new ModelAndView("institution/InstitutionList");
//				mav.addObject("branchList", branchList);
			} else {
				mav = new ModelAndView("institution/PublicInstitutionDetail");
			}
			mav.addObject("branchMap", branchMap);
			Institution institution = oInstitutionService.get(id);
			List<String> imageList = null;
			if (institution.getImages() != null) {
				imageList = Splitter.on(";").omitEmptyStrings().splitToList(institution.getImages());
				mav.addObject("imageList", imageList);
			}
			mav.addObject("bean", institution);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return mav;
	}
}
